package com.zyd.blog.controller;

/**
 * 页面渲染相关 -- 页面跳转
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/24 14:37
 * @since 1.0
 */

import com.zyd.blog.business.annotation.BussinessLog;
import com.zyd.blog.business.entity.Article;
import com.zyd.blog.business.enums.ConfigKeyEnum;
import com.zyd.blog.business.service.BizArticleService;
import com.zyd.blog.business.service.SysConfigService;
import com.zyd.blog.core.websocket.server.ZydWebsocketServer;
import com.zyd.blog.util.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 页面跳转类
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/24 14:37
 * @since 1.0
 */
@Controller
public class RenderForUserController {

    @Autowired
    private BizArticleService articleService;
    @Autowired
    private SysConfigService configService;
    @Autowired
    private ZydWebsocketServer websocketServer;

    @RequiresPermissions("resources")
    @BussinessLog("进入资源列表页")
    @GetMapping("/user/resources")
    public ModelAndView resources() {
        return ResultUtil.view("resources/user/list");
    }

    @RequiresPermissions("articles")
    @BussinessLog("进入文章列表页")
    @GetMapping("/user/articles")
    public void articles(HttpServletResponse response) throws IOException {
        Map config = configService.getConfigs();
        String siteUrl = (String) config.get(ConfigKeyEnum.SITE_URL.getKey());
        response.sendRedirect(siteUrl + "/myarticles");
        return;
    }

    @RequiresPermissions("article:publish")
    @BussinessLog(value = "进入发表文章页[html]")
    @GetMapping("/user/article/publish")
    public ModelAndView publish() {
        return ResultUtil.view("article/user/publish");
    }

    @RequiresPermissions("article:publish")
    @BussinessLog(value = "进入发表文章页[markdown]")
    @GetMapping("/user/article/publishMd")
    public ModelAndView publishMd() {
        return ResultUtil.view("article/user/publish-md");
    }

    @RequiresPermissions("article:publish")
    @BussinessLog(value = "进入修改文章页[id={1}]")
    @GetMapping("/user/article/update/{id}")
    public ModelAndView edit(@PathVariable("id") Long id, Model model) {
        model.addAttribute("id", id);
        Article article = articleService.getByPrimaryKey(id);
        if(article.getIsMarkdown()){
            return ResultUtil.view("article/user/publish-md");
        }
        return ResultUtil.view("article/user/publish");
    }

    @RequiresPermissions("comments")
    @BussinessLog("进入评论页")
    @GetMapping("/user/comments")
    public ModelAndView comments() {
        return ResultUtil.view("comment/user/list");
    }
}
